<?php

namespace App\Http\Controllers;

use App\AdvertiserImg;
use App\AdvertiserVideo;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class YdCallbackController extends Controller
{
    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function image(Request $request)
    {
        $data = $request->all();
        Log::info('易盾图片回调', [
            'param' => $data
        ]);

//        $data = json_decode('{"signature":"9f990f5ab5dd3287df472dc05b0ce3de","callbackData":"{\"antispam\":{\"taskId\":\"jzqqtxll2n32n86edjc4jn0h0030aa07\",\"status\":2,\"suggestion\":0,\"censorType\":0,\"name\":\"12889981337\",\"dataId\":\"12889981337\",\"labels\":[{\"label\":500,\"level\":0,\"rate\":0.99},{\"label\":300,\"level\":0,\"rate\":1.0},{\"label\":100,\"level\":0,\"rate\":0.9994},{\"label\":400,\"level\":0,\"rate\":1.0},{\"label\":110,\"level\":0,\"rate\":0.9989},{\"label\":800,\"level\":0,\"rate\":0.9566},{\"label\":260,\"level\":0,\"rate\":1.0},{\"label\":1100,\"level\":0,\"rate\":0.9566},{\"label\":900,\"level\":0,\"rate\":1.0}],\"resultType\":1,\"censorTime\":1726300176247,\"url\":\"https://yidun-antispam.nos-eastchina1.126.net/9d61a1b166d63572bcea486ee9ead439?Signature=GoQskdzgwntdZFZizykH%2BKoYB9PcS3gHoMHqSc714Y8%3D&Expires=1726321776&NOSAccessKeyId=a681ebcbcd5646a488d275c33acb16bd\",\"frameSize\":1,\"hitSource\":0,\"imgMd5\":\"9d61a1b166d63572bcea486ee9ead439\",\"label\":0}}","businessId":"7e0bd3f170a589dfd22f3834c0456e49","secretId":"6e3413c348df7ea3afd7500919bc07df"}', true);
        if (isset($data['callbackData']) && $data['callbackData']){
            $data['callbackData'] = json_decode($data['callbackData'] , true);
            $dataId = $data['callbackData']['antispam']['dataId'] ?? '';
            if (!empty($dataId)){
                $labelsArr = [];
                if (isset($data['callbackData']['antispam']['labels'])  && $data['callbackData']['antispam']['labels']){
                    foreach ($data['callbackData']['antispam']['labels'] as $label){
                        if (isset($label['level']) && $label['level'] > 0){
                            $labelsArr[] = [
                                'label' => $label['label'],
                                'tagName' => AdvertiserImg::LABEL[$label['label']] ?? ''
                            ];
                        }
                    }
                }
                $model = AdvertiserImg::query()
                    ->where('image_id', $dataId)
                    ->where('status', 1)
                    ->first();
                if ($model){
                    $model->status = 2;
                    $model->suggestion = $data['callbackData']['antispam']['suggestion'] ?? 0;
                    $model->censor_time = Carbon::now();
                    $model->label = json_encode($labelsArr);
                    $model->save();
                }
            }
        }

        return response()->json([
            'code' =>200,
            'msg' => '接收成功'
        ]);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function video(Request $request)
    {
        $data = $request->all();
        Log::info('易盾视频回调', [
            'param' => $data
        ]);

//        $data = json_decode('{"signature":"e01ff90535ac9f48049275e700d35576","callbackData":"{\"dataId\":\"12914118231\",\"taskId\":\"ajhpajqcdpgt7zmhmx0iy70h0200aa08\",\"result\":1,\"checkStatus\":1,\"censorSource\":2,\"strategySource\":0,\"censorTime\":1726303312041,\"checkTime\":1726303350651,\"duration\":71007,\"durationMs\":71007,\"censorLabels\":[]}","secretId":"8401431ec01bbb9195d76c8b68968c76"}', true);
//        $data = json_decode('{"signature":"8499fedc8d7e297d108b38bed88d088b","callbackData":"{\"dataId\":\"12925157617\",\"taskId\":\"t996njzdjvvafg6kt9ro540h0200aa08\",\"result\":3,\"checkStatus\":1,\"censorSource\":2,\"strategySource\":0,\"censorTime\":1726303312068,\"checkTime\":1726303355692,\"duration\":57586,\"durationMs\":57586,\"censorLabels\":[],\"evidences\":{\"video\":{\"taskId\":\"t996njzdjvvafg6kt9ro540h0200aa08\",\"status\":0,\"level\":1,\"suggestReason\":110,\"evidences\":[{\"type\":1,\"url\":\"https://yidun-illegal-video.vod.126.net/t996njzdjvvafg6kt9ro540h0200aa08_5_25000.jpg?Signature=5prUXu1CF%2BcpFy3pmSciK6%2BARH6HIAJFUtYH9pF3OXk%3D&Expires=1728895355&NOSAccessKeyId=23206c563a7147f39f11aedc36e8b708\",\"beginTime\":25000,\"endTime\":25000,\"labels\":[{\"label\":110,\"level\":1,\"rate\":0.616705,\"subLabels\":[{\"subLabel\":11017,\"subLabelPath\":[\"11017\"],\"rate\":0.6167045,\"labelLevel\":0,\"hitStrategy\":0,\"details\":{\"hitInfos\":[\"胸部非裸露特\"],\"imageTagInfos\":[{\"tagName\":\"胸部非裸露特写\",\"tagGroup\":\"性感低俗\"}]}}]}],\"frontPics\":[{\"url\":\"https://yidun-illegal-video.vod.126.net/t996njzdjvvafg6kt9ro540h0200aa08_2_10000.jpg?Signature=17x8RE10xd%2BuSbXwHAd6MOkHkSooFFXEOHj5pq4y7zQ%3D&Expires=1728895355&NOSAccessKeyId=23206c563a7147f39f11aedc36e8b708\"},{\"url\":\"https://yidun-illegal-video.vod.126.net/t996njzdjvvafg6kt9ro540h0200aa08_3_15000.jpg?Signature=rKsTpCihw9xIHn%2FAi%2BY0fYrP5e5k7cw1t9vrtZAlQfA%3D&Expires=1728895355&NOSAccessKeyId=23206c563a7147f39f11aedc36e8b708\"},{\"url\":\"https://yidun-illegal-video.vod.126.net/t996njzdjvvafg6kt9ro540h0200aa08_4_20000.jpg?Signature=7TEyzV0oguhKxbyt59ZxTZ8lunvJ41fzJdosKtrXtbw%3D&Expires=1728895355&NOSAccessKeyId=23206c563a7147f39f11aedc36e8b708\"}],\"backPics\":[{\"url\":\"https://yidun-illegal-video.vod.126.net/t996njzdjvvafg6kt9ro540h0200aa08_6_30000.jpg?Signature=6Qlump770v51c%2BNxjUWXX43jqqcRXFZBbIXtL6XQFBY%3D&Expires=1728895355&NOSAccessKeyId=23206c563a7147f39f11aedc36e8b708\"},{\"url\":\"https://yidun-illegal-video.vod.126.net/t996njzdjvvafg6kt9ro540h0200aa08_7_35000.jpg?Signature=vd19Cq%2FYnluXIEDG1cYqXbO0YF9dToBuxOPecFHrg0s%3D&Expires=1728895355&NOSAccessKeyId=23206c563a7147f39f11aedc36e8b708\"},{\"url\":\"https://yidun-illegal-video.vod.126.net/t996njzdjvvafg6kt9ro540h0200aa08_8_40000.jpg?Signature=qgTqL4UflJ0McwfxKrpAYu4FnQ%2FO2q6%2FO2DNTmLrF0U%3D&Expires=1728895355&NOSAccessKeyId=23206c563a7147f39f11aedc36e8b708\"}]}]}}}","secretId":"8401431ec01bbb9195d76c8b68968c76"}', true);

        if (isset($data['callbackData']) && $data['callbackData']){
            $data['callbackData'] = json_decode($data['callbackData'] , true);
            $dataId = $data['callbackData']['dataId'] ?? '';
            if (!empty($dataId)){

                $item = $data['callbackData'];

                $labelVideo = $labelVoice = [];
                if (isset($item['evidences']['video']) && $item['evidences']['video']){
                    if ($item['evidences']['video']['level'] > 0){
                        foreach ($item['evidences']['video']['evidences'] as $evidence){
                            foreach ($evidence['labels'] as $l){
                                foreach ($l['subLabels'] as $subLabel){
                                    if (isset($subLabel['details']['imageTagInfos']) && $subLabel['details']['imageTagInfos']) {
                                        foreach ($subLabel['details']['imageTagInfos'] as $info){
                                            $labelVideo[] = [
                                                'tagGroup' => $info['tagGroup'],
                                                'tagName' => $info['tagName']
                                            ];
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                if (isset($item['evidences']['audio']) && $item['evidences']['audio']) {
                    if (isset($item['evidences']['audio']['label']) && $item['evidences']['audio']['label'] > 0) {
                        $labelVoice[] = [
                            'tagGroup' => '',
                            'tagName' => AdvertiserVideo::LABEL[$item['evidences']['audio']['label']] ?? ''
                        ];
                    }
                }


                $model = AdvertiserVideo::query()
                    ->where('video_id', $dataId)
                    ->where('status', 1)
                    ->first();
                if ($model){
                    $suggestReason = $item['result'] == 2 ? 4 : $item['result'];
                    $model->status = 2;
                    $model->suggestion = $suggestReason;
                    $model->censor_time = Carbon::now();
                    $model->label = json_encode([
                        'video' => $labelVideo,
                        'audio' => $labelVoice
                    ]);
                    $model->save();
                }
            }
        }

        return response()->json([
            'code' =>200,
            'msg' => '接收成功'
        ]);
    }
}
